/***********************************************************************
    Originally Written By Max McGuire

    Modified By Alex "SparkyMcSparks" Romo

    Questions? Concerns? Suggestions?
        Contact Me On AIM @ DareDevilSparks7
***********************************************************************/

Script.Load( "lua/Utility.lua" )

class 'Game' (Entity)

Game.updateInterval = 1
Game.networkVars =
    {
        startTime = "float",
        aiLimit = "integer"
    }
    
function Game:OnCreate()
    Entity.OnCreate(self)

    Game.instance = self

    if (Server) then
        self:SetPropagate(Entity.Propagate_Always)
        self:SetNextThink(self.updateInterval)

        self.enemy_spawns = getEnt( "target" )
        self.enemy_spawns_num = table.maxn( self.enemy_spawns )
    end

    // Player Vars
    self.player = nil
    self.playerShields = 2
    self.playerLives = 3

    // Game Vars
    self.level = 1
    self.startTime = 1.0 // Seconds
    self.aiLimit = 32 // Default Will Be This
    self.gameOver = false
    self.gameActive = true
    self.gameSpeed = 5
    self.numTargets = 0
    
    // Score Vars
    self.type1Points = 10
    self.type2Points = 20
    self.type3Points = 30
    self.type4Points = 40
end

/*
Returns AI Limit
*/
function GetAILimit()
    return self.aiLimit
end

/*
Sets AI Limit
*/
function SetAILimit( num )
    self.aiLimit = num
end

/*
Time Since Game Started
*/
function Game:GetGameTime()
    if (self.startTime ~= 0) then
        return Shared.GetTime() - self.startTime
    else
        return 0
    end
end

/*
Check If Game Started
*/
function Game:GetHasGameStarted()
    return self:GetGameTime() > 0
end

if (Server) then
    function Game:StartGame()
        self.startTime = Shared.GetTime() + self.startTime
    end

    function Game:OnThink()
        // Has Game Started
        if (self.startTime > 0 and Shared.GetTime() > self.startTime) then
            if ( self.numTargets < 12 and self.enemy_spawns_num > 0) then
                local spawnIndex  = math.random(3, self.enemy_spawns_num)
                local spawnOrigin = self.enemy_spawns[ spawnIndex ]:GetOrigin()
                local spawnAngles = self.enemy_spawns[ spawnIndex ]:GetAngles()

                local target = Server.CreateEntity( "galaga_target",  spawnOrigin )
                target:SetAngles( spawnAngles )
                target:MoveIn()

                self.numTargets = self.numTargets + 1
            end
        end
        
        self:SetNextThink(self.updateInterval)
    end

    /*
    Destroy + Award Points
    */
    function Game:DestroyTarget( player, target )
        self.player = player
        self.numTargets = self.numTargets - 1
        
        assertEx( target.type, "Target type not defined!" )
        
        player.old_score = player.score
        
        if( target.type == 1 ) then
            Galaga_Scoring.instance:AddScore( player, self.type1Points )
        elseif( target.type == 2 ) then
            Galaga_Scoring.instance:AddScore( player, self.type2Points )
        elseif( target.type == 3 ) then
            Galaga_Scoring.instance:AddScore( player, self.type3Points )
        elseif( target.type == 4 ) then
            Galaga_Scoring.instance:AddScore( player, self.type4Points )
        else // Fail Safe
            assertMsg( "No Target typed defined, defaulting score value." )
            Galaga_Scoring.instance:AddScore( player, self.type1Points )
        end
    end
end

Shared.LinkClassToMap( "Game", "game", Game.networkVars )